package com.lt.lumentracks.service.Impl;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lt.lumentracks.entity.po.Topic;
import com.lt.lumentracks.entity.vo.common.PageVO;
import com.lt.lumentracks.mapper.TopicMapper;
import com.lt.lumentracks.service.ITopicService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 热门话题表 服务实现类
 * </p>
 *
 * @author swl
 * @since 2025-07-23
 */
@Service
public class TopicServiceImpl extends ServiceImpl<TopicMapper, Topic> implements ITopicService {

    // 分页获取所有话题
    @Override
    public PageVO<Topic> getAllTopicByPage(long current, long size) {
        Page<Topic> p = new Page<>(current, size);
        p.addOrder(OrderItem.desc("heat_score"));

        Page<Topic> page_MP = this.page(p);
        return PageVO.of(page_MP);
    }


    // 根据传入的话题id进行查询
    @Override
    public List<Topic> getTopicByIds(List<Integer> ids) {
        // 1. 查询所有ID对应的Topic，并用Map存储以便快速查找
        Map<Integer, Topic> topicMap = this.listByIds(ids)
                .stream()
                .collect(Collectors.toMap(Topic::getId, topic -> topic));

        // 2. 按照原始ids顺序构建结果列表
        return ids.stream()
                .map(topicMap::get)
                .filter(Objects::nonNull) // 过滤掉不存在的ID
                .collect(Collectors.toList());
    }
}
